<template>
  <!-- 新增dialog -->
  <el-dialog title="新增员工" :visible="dialogvisible" @close="closeDialog" @open="openDialog">
    <!-- 表单 -->
    <el-form ref="formRef" :model="formData" label-width="100px" :rules="rules">
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="formData.username" placeholder="1-30个字符" style="width:80%" />
      </el-form-item>
      <el-form-item label="手机：" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" style="width:80%" />
      </el-form-item>
      <el-form-item label="入职时间：" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期" style="width: 80%;" />
      </el-form-item>
      <el-form-item label="聘用形式：" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in hireList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号:" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="1-20个字符" style="width:80%" />
      </el-form-item>
      <el-form-item label="部门:" prop="departmentName">
        <el-cascader
          v-model="formData.departmentName"
          :options="departList"
          :props="departProps"
          :show-all-levels="false"
          clearable
        />
      </el-form-item>
      <el-form-item label="转正时间：" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" type="date" placeholder="选择日期" style="width: 80%;" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="addEL">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { fetchAddEmployee } from '@/api/employees'
import transTree from '@/utils/transTree'
export default {
  props: {
    dialogvisible: {
      type: Boolean
    }
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: ['blur', 'change'] }
        ]
      },
      hireList: [// 招聘方式 value： 1 / 2
        {
          id: 1,
          name: '正式'
        },
        {
          id: 2,
          name: '非正式'
        }
      ],
      departProps: {
        label: 'name',
        value: 'name',
        checkStrictly: true
      },
      departList: [] // 部门列表
    }
  },
  methods: {
    // 获取部门列表
    async fetchDepartment() {
      const res = await getDepartments()
      // 把后端返回的平铺数组转化成页面需要的树形数组
      this.departList = transTree(res.depts)
    },
    // 关闭对话框
    closeDialog() {
      this.$refs.formRef.resetFields()
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$emit('close-dialog')
    },
    openDialog() {
      this.fetchDepartment()
    },
    // 新增
    addEL() {
      console.log(11111)
      this.$refs.formRef.validate(async vaild => {
        if (vaild) {
          const length = this.formData.departmentName.length
          const departmentName = this.formData.departmentName[length - 1]
          await fetchAddEmployee({ ...this.formData, departmentName })
          this.$message.success('新增员工成功')
          this.closeDialog()
          this.$emit('add-employee')
        }
      })
    }
  }

}
</script>

